const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: null,
    trainId: null,
    finishTrain: false,
    courseTrains: [],
    baseData: [{
      name: '胸部训练',
      items: []
    }, {
      name: '背部训练',
      items: []
    }, {
      name: '手臂训练',
      items: []
    }, {
      name: '臀部训练',
      items: []
    }, {
      name: '腹部训练',
      items: []
    }, {
      name: '腿部训练',
      items: []
    }, {
      name: '减脂训练',
      items: []
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const phone = wx.getStorageSync("phone");
    this.setData({
      phone
    });
  },

  initData: function() {
    const that = this;
    const phone = wx.getStorageSync("phone");
    const url = app.globalData.url;
    const baseData = that.data.baseData;
    const baseUrl = app.globalData.baseUrl;
    wx.showNavigationBarLoading();
    wx.request({
      url: url + '/train/getTodayTrains/' + phone,
      success: function(response) {
        wx.hideNavigationBarLoading();
        if (response.data.success) {
          const trainObj = response.data.content;
          that.setData({
            trainId: trainObj.id
          });
          const coTrains = trainObj.courseTrains;
          if (coTrains && coTrains.length > 0) {
            const courseTrains = [];
            baseData.forEach(function(item) {
              coTrains.forEach(function(course) {
                if (course.name === item.name) {
                  item.id = course.id;
                  item.items = course.items;
                  switch (item.name) {
                    case '胸部训练':
                      item.cover_image = baseUrl + '/file/xb.jpg';
                      break;
                    case '背部训练':
                      item.cover_image = baseUrl + '/file/bb.jpg';
                      break;
                    case '手臂训练':
                      item.cover_image = baseUrl + '/file/sb.jpg';
                      break;
                    case '臀部训练':
                      item.cover_image = baseUrl + '/file/tb.jpg';
                      break;
                    case '腹部训练':
                      item.cover_image = baseUrl + '/file/fb.jpg';
                      break;
                    case '腿部训练':
                      item.cover_image = baseUrl + '/file/tuib.jpg';
                      break;
                    case '减脂训练':
                      item.cover_image = baseUrl + '/file/jz.jpg';
                      break;
                  }
                  courseTrains.push(item);
                }
              });
            });
            that.setData({
              courseTrains,
              finishTrain: trainObj.finishTrain
            });
          }
        } else {
          wx.showToast({
            title: '获取课程失败',
            icon: 'none'
          })
        }
      }
    })
  },

  onShow: function() {
    this.initData();
  },
  editTrain: function(e) {
    const that = this;
    const url = app.globalData.url;
    const index = e.currentTarget.dataset.index;
    const courseTrains = that.data.courseTrains;
    const selectTrain = courseTrains[index];
    wx.setStorageSync("courseId", selectTrain.id);
    wx.setStorageSync("courseName", selectTrain.name);
    wx.setStorageSync('traineePhone', this.data.phone);
    wx.setStorageSync("finishTrain", that.data.finishTrain);
    wx.navigateTo({
      url: '/pages/courseTrainDetail/courseTrainDetail',
    })
  },
  delTrain: function(e) {
    const that = this;
    const url = app.globalData.url;
    const index = e.currentTarget.dataset.index;
    const courseTrains = that.data.courseTrains;
    const selectCourse = courseTrains[index];
    wx.showModal({
      title: '提示',
      content: '确定删除课程？',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: url + '/train/delCourseTrain/' + selectCourse.id,
            success: function(response) {
              if (response.data.success) {
                that.setData({
                  courseTrains: []
                });
                wx.showToast({
                  title: '删除课程成功',
                  icon: 'none'
                });
                that.initData();
              } else {
                wx.showToast({
                  title: '删除课程失败',
                  icon: 'none'
                });
              }
            }
          })
        }
      }
    })
  },
  addCourse: function() {
    const trainId = this.data.trainId;
    wx.setStorageSync("trainId", trainId);
    const phone = wx.getStorageSync("phone");
    wx.navigateTo({
      url: '/pages/courseSelection/courseSelection'
    })
  },
  finishCourse: function() {
    const trainId = this.data.trainId;
    wx.setStorageSync("trainId", trainId);
    wx.navigateTo({
      url: '/pages/trainFeeling/trainFeeling',
    })
  }
})